//
//  GROrderManagerListViewController.m
//  YC_GRice
//
//  Created by luojinrong11 on 2018/7/20.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GROrderManagerListViewController.h"
#import "UIViewController+BackButtonHandler.h"
#import "MSTabBarController.h"
#import "GRRiceOrderListVC.h"
#define titleW 80
#define titleH 35

@interface GROrderManagerListViewController ()<UIScrollViewDelegate>
{
    NSInteger currentIndex;

}
@property (weak, nonatomic) IBOutlet UIScrollView *titleScrollerView;

@property (weak, nonatomic) IBOutlet UIScrollView *mainScrollerVew;
@property(nonatomic,strong)NSArray * titleArr;
@property(nonatomic,strong)UIView * lineView;
@property(nonatomic,weak)UIButton * selectedBt;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topOffset;

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@end

@implementation GROrderManagerListViewController

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (self.isRiceList && self.isTabBar) {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.isRiceList && self.isTabBar) {
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    if (self.isRiceList && self.isTabBar) {
        self.topOffset.constant = [UIApplication sharedApplication].statusBarFrame.size.height;
    }else {
        self.topOffset.constant = IS_IPHONEX?88:64;
    }
    currentIndex = _seletedType;
    [self setupUI];

}

-(BOOL)navigationShouldPopOnBackButton{
    if (_isPay) {
          [MSTabBarController seletedTabIndex:2];
    }
    return YES;
}
-(void)setupUI{
    if (_isRiceList) {
        self.titleArr = @[@"全部",@"待付款",@"可使用",@"已消费",@"退款/过期"];
        self.titleLabel.text = @"同城优惠";
        
    }else{
         self.titleArr = @[@"全部",@"待付款",@"待发货",@"待收货",@"已完成",@"已取消"];
         self.titleLabel.text = @"我的订单";
    }
   
    self.titleScrollerView.contentSize = CGSizeMake(self.titleArr.count * titleW, titleH);
    self.mainScrollerVew.contentSize = CGSizeMake(self.titleArr.count * KscreenWidth, self.mainScrollerVew.bounds.size.height);
    for (int i = 0; i < self.titleArr.count; i++) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i *titleW, 0, titleW, titleH);
        [button setTitle:self.titleArr[i] forState:UIControlStateNormal];
        [button setTitleColor:HEXCOLOR(0x666666) forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button addTarget:self action:@selector(changeLineFrame:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:HEXCOLOR(0x222222) forState:UIControlStateSelected];
        button.tag = 1000+i;
        [self.titleScrollerView addSubview:button];
        if (i == currentIndex ) {
            self.mainScrollerVew.contentOffset = CGPointMake(currentIndex*KscreenWidth, 0);
            self.lineView = [[UIView alloc]initWithFrame:CGRectMake(0, titleH -3, 30, 3)];
            _lineView.centerX = button.centerX;
            _lineView.backgroundColor = HEXCOLOR(0xFBD62D);
            [self.titleScrollerView addSubview:self.lineView];
            button.selected = YES;
            _selectedBt = button;
        }
        if (_isRiceList) {
            GRRiceOrderListVC * riceOrderListVC = [[GRRiceOrderListVC alloc]initWithStyle:UITableViewStylePlain];
            riceOrderListVC.orderType = i;
            riceOrderListVC.view.frame = CGRectMake(i*KscreenWidth, 0, self.mainScrollerVew.bounds.size.width, self.mainScrollerVew.bounds.size.height);
            [self.mainScrollerVew addSubview:riceOrderListVC.view];
            [self addChildViewController:riceOrderListVC];
            
            
        }else{

        }
    }
}

-(void)changeLineFrame:(UIButton *)button{

  [self.mainScrollerVew setContentOffset:CGPointMake((button.tag-1000)*KscreenWidth, 0) animated:YES];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if ([scrollView isEqual:self.mainScrollerVew]) {
        NSInteger index = (int) (scrollView.contentOffset.x/KscreenWidth +0.5);
        self.lineView.centerX =  titleW/2 + scrollView.contentOffset.x/(KscreenWidth/titleW);
        _selectedBt.selected = NO;
        UIButton * button = (UIButton *)[self.titleScrollerView viewWithTag:1000+index];
        button.selected = YES;
        _selectedBt = button;
        currentIndex = index;
        if (_selectedBt.x +_selectedBt.width >  self.titleScrollerView.bounds.size.width) {
            [self.titleScrollerView setContentOffset: CGPointMake((_selectedBt.x +_selectedBt.width) - self.titleScrollerView.bounds.size.width, 0)animated:YES] ;
        }else{
            [self.titleScrollerView setContentOffset:CGPointZero animated:YES];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
